package com.twu.biblioteca;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;

public class MovieList {
    private ArrayList<Movie> movies = new ArrayList<Movie>();

    public MovieList() {

    }

    public MovieList(String xmlFilePath) {
        parse(xmlFilePath);
    }

    public int getMoviesCount() {
        return movies.size();
    }

    public void add(Movie movie) {
        movies.add(movie);
    }

    public Movie getMovieByNumber(int number) {
        for (int i = 0; i < getMoviesCount(); i++) {
            Movie movie = movies.get(i);
            if (movie.getNumber() == number)
                return movie;
        }
        return null;
    }

    public Movie removeMovieByNumber(int number) {
        Movie removedMovie = null;

        for (int i = 0; i < getMoviesCount(); i++) {
            Movie movie = this.movies.get(i);
            if (movie.getNumber() == number) {
                removedMovie = movie;
                break;
            }
        }
        if (removedMovie != null) {
            this.movies.remove(removedMovie);
        }

        return removedMovie;
    }

    private void parse(String xmlFilePath) {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        Document document = null;
        Element rootElement = null;

        try {
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            document = builder.parse(new File(xmlFilePath));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        rootElement = document.getDocumentElement();
        NodeList movieList = rootElement.getElementsByTagName("Movie");

        if (movieList != null) {
            for (int i = 0; i < movieList.getLength(); i++) {
                Node movieNode = movieList.item(i);
                NodeList infoNodeList = movieNode.getChildNodes();
                int number = Integer.parseInt(infoNodeList.item(1).getTextContent());
                String name = infoNodeList.item(3).getTextContent();
                int year = Integer.parseInt(infoNodeList.item(5).getTextContent());
                String director = infoNodeList.item(7).getTextContent();
                int rating = infoNodeList.item(9).getTextContent().equals("") ? -1 : Integer.parseInt(infoNodeList.item(9).getTextContent());

                add(new Movie(number, name, year, director, rating));
            }
        }
    }

    public void printMovieList() {
        printMovieListHead();
        printAllMoviesInformation();
        printMovieListEnd();
    }

    public void printMovieListHead() {
        String movieListHead = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" +
                "                                     The Movie List\n" +
                "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
        System.out.println(movieListHead);
    }

    public void printAllMoviesInformation() {
        int size = getMoviesCount();

        if (size == 0) {
            System.out.println("  The movie list is empty\n");
        } else {
            System.out.println(String.format("%s    %-30s    %s    %-20s    %s\n", "Number", "Name", "Year", "Director", "Rating"));
            for (int i = 0; i < size; i++) {
                this.movies.get(i).printInformation();
            }
        }
    }

    public void printMovieListEnd() {
        System.out.println("++++++++++++++++++++++++++++++++++++++  End  +++++++++++++++++++++++++++++++++++++++\n");
    }
}
